﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.Entities;
using OreSystem.BLL;
namespace MainFrom
{
    public partial class LevelForm : Form
    {
        private LevelBll levelBLL = new LevelBll();
        public int layer { get; set; }
        public LevelForm()
        {
            InitializeComponent();
        }
        private void LevelForm_Load(object sender, EventArgs e) {
            if (this.layer == 1) {
                this.Text = "二"+this.Text;
            }
            if (this.layer == 2) {
                this.Text = "三" + this.Text;
            }
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.Columns.Add("ID", "序号");
            dataGridView1.Columns.Add("Layer", "层级");
            dataGridView1.Columns.Add("PID", "上层");
            dataGridView1.Columns.Add("LevelNumber", "层编号");
            dataGridView1.Columns.Add("Name", "负责人");
            dataGridView1.Columns.Add("Path", "路径");
            dataGridView1.Columns.Add("CreateDate", "添加时间");
            dataGridView1.Columns[0].DataPropertyName = "ID";
            dataGridView1.Columns[1].DataPropertyName = "Layer";
            dataGridView1.Columns[2].DataPropertyName = "PID";
            dataGridView1.Columns[3].DataPropertyName = "LevelNumber";
            dataGridView1.Columns[4].DataPropertyName = "Name";
            dataGridView1.Columns[5].DataPropertyName = "Path";
            dataGridView1.Columns[6].DataPropertyName = "CreateDate";

            dataGridView1.DataSource = levelBLL.GetLevelByLevel(this.layer);
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.MultiSelect = false;
            dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
            
        }

        private void toolStripButton1_Click(object sender, EventArgs e) {
            LevelEdit frm_levelEdit = new LevelEdit();
            frm_levelEdit.layer = this.layer;
            if (this.layer == 1)
            {
                frm_levelEdit.PLevel = new List<Level> { new Level { ID = 0, Name = "顶层" } };
            }
            else if (this.layer == 2)
            {
                frm_levelEdit.PLevel = levelBLL.GetLevelByLevel(this.layer - 1);
            }
          
            if (frm_levelEdit.ShowDialog() == DialogResult.OK) {
                dataGridView1.DataSource = levelBLL.GetLevelByLevel(this.layer);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e) {
            LevelEdit frm_levelEdit = new LevelEdit();
            if (dataGridView1.SelectedRows.Count <=0)
            {
                MessageBox.Show("请选择要修改的数据！");
                return;
            }
            int id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
            frm_levelEdit.newLevel = levelBLL.GetLevelById(id);
            if (this.layer == 1)
            {
                frm_levelEdit.PLevel = new List<Level> { new Level { ID = 0, Name = "顶层" } };
            }
            else if (this.layer == 2)
            {
                frm_levelEdit.PLevel = levelBLL.GetLevelByLevel(this.layer - 1);
            }          
            frm_levelEdit.layer = this.layer;
            if (frm_levelEdit.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                
            }
            dataGridView1.DataSource = levelBLL.GetLevelByLevel(this.layer);
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请选择要修改的数据！");
                return;
            }
            int id = (int)dataGridView1.CurrentRow.Cells[0].Value;
            if (MessageBox.Show("确定删除吗？","删除提示",MessageBoxButtons.OKCancel)== System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    levelBLL.DeleteByID(id);
                    dataGridView1.DataSource = levelBLL.GetLevelByLevel(this.layer);
                }
                catch (Exception ex)
                {

                    MessageBox.Show("出现错误请联系管理员。错误信息" + ex.Message);
                }
                
                
            }
            

        }

        private void btn_search_Click(object sender, EventArgs e) {
            dataGridView1.DataSource = levelBLL.GetListByKey(txt_Keys.Text,this.layer);
        }
    }
}
